clc
close all
clear all

%%%==============================================================================================
%%                  Draw the PRK model parameter K (Y-axis is in log scale)

[sender70_rd1, receiver70_rd1,erinterference70_rd1] = textread('MIXED70_interference_rd1.txt', '%d%d%f');
[sender70_rd2, receiver70_rd2,erinterference70_rd2] = textread('MIXED70_interference_rd2.txt', '%d%d%f');
[sender70_rd3, receiver70_rd3,erinterference70_rd3] = textread('MIXED70_interference_rd3.txt', '%d%d%f');
[sender70_rd4, receiver70_rd4,erinterference70_rd4] = textread('MIXED70_interference_rd4.txt', '%d%d%f');
[sender70_rd5, receiver70_rd5,erinterference70_rd5] = textread('MIXED70_interference_rd5.txt', '%d%d%f');
erinterference70 = [erinterference70_rd1;erinterference70_rd2;erinterference70_rd3;erinterference70_rd4;erinterference70_rd5;];
sender70 = [sender70_rd1;sender70_rd2;sender70_rd3;sender70_rd4;sender70_rd5;];
receiver70 = [receiver70_rd1;receiver70_rd2;receiver70_rd3;receiver70_rd4;receiver70_rd5;];

[sender80_rd1, receiver80_rd1,erinterference80_rd1] = textread('MIXED80_interference_rd1.txt', '%d%d%f');
[sender80_rd2, receiver80_rd2,erinterference80_rd2] = textread('MIXED80_interference_rd2.txt', '%d%d%f');
[sender80_rd3, receiver80_rd3,erinterference80_rd3] = textread('MIXED80_interference_rd3.txt', '%d%d%f');
[sender80_rd4, receiver80_rd4,erinterference80_rd4] = textread('MIXED80_interference_rd4.txt', '%d%d%f');
[sender80_rd5, receiver80_rd5,erinterference80_rd5] = textread('MIXED80_interference_rd5.txt', '%d%d%f');
erinterference80 = [erinterference80_rd1;erinterference80_rd2;erinterference80_rd3;erinterference80_rd4;erinterference80_rd5;];
sender80 = [sender80_rd1;sender80_rd2;sender80_rd3;sender80_rd4;sender80_rd5;];
receiver80 = [receiver80_rd1;receiver80_rd2;receiver80_rd3;receiver80_rd4;receiver80_rd5;];

[sender90_rd1, receiver90_rd1,erinterference90_rd1] = textread('MIXED90_interference_rd1.txt', '%d%d%f');
[sender90_rd2, receiver90_rd2,erinterference90_rd2] = textread('MIXED90_interference_rd2.txt', '%d%d%f');
[sender90_rd3, receiver90_rd3,erinterference90_rd3] = textread('MIXED90_interference_rd3.txt', '%d%d%f');
[sender90_rd4, receiver90_rd4,erinterference90_rd4] = textread('MIXED90_interference_rd4.txt', '%d%d%f');
[sender90_rd5, receiver90_rd5,erinterference90_rd5] = textread('MIXED90_interference_rd5.txt', '%d%d%f');
erinterference90 = [erinterference90_rd1;erinterference90_rd2;erinterference90_rd3;erinterference90_rd4;erinterference90_rd5;];
sender90 = [sender90_rd1;sender90_rd2;sender90_rd3;sender90_rd4;sender90_rd5;];
receiver90 = [receiver90_rd1;receiver90_rd2;receiver90_rd3;receiver90_rd4;receiver90_rd5;];


[sender95_rd1, receiver95_rd1,erinterference95_rd1] = textread('MIXED95_interference_rd1.txt', '%d%d%f');
[sender95_rd2, receiver95_rd2,erinterference95_rd2] = textread('MIXED95_interference_rd2.txt', '%d%d%f');
[sender95_rd3, receiver95_rd3,erinterference95_rd3] = textread('MIXED95_interference_rd3.txt', '%d%d%f');
[sender95_rd4, receiver95_rd4,erinterference95_rd4] = textread('MIXED95_interference_rd4.txt', '%d%d%f');
[sender95_rd5, receiver95_rd5,erinterference95_rd5] = textread('MIXED95_interference_rd5.txt', '%d%d%f');
erinterference95 = [erinterference95_rd1;erinterference95_rd2;erinterference95_rd3;erinterference95_rd4;erinterference95_rd5;];
sender95 = [sender95_rd1;sender95_rd2;sender95_rd3;sender95_rd4;sender95_rd5;];
receiver95 = [receiver95_rd1;receiver95_rd2;receiver95_rd3;receiver95_rd4;receiver95_rd5;];


PATH_LOSS_EXPONENT=2.6;
results = {0};
[m,n] = size(erinterference70);
K70=[];
for i=1:1:m
    rxPowerDbm =CalcRxPower(CalcDistance(sender70(i,1), receiver70(i,1)), PATH_LOSS_EXPONENT); 
    rxPowerW = realpow(10, rxPowerDbm/10)/1000;
    K70 = [K70 rxPowerW/erinterference70(i,1)];
        end
results{1} = K70';

[m,n] = size(erinterference80);
K80=[];
for i=1:1:m
    rxPowerDbm =CalcRxPower(CalcDistance(sender80(i,1), receiver80(i,1)), PATH_LOSS_EXPONENT); 
    rxPowerW = realpow(10, rxPowerDbm/10)/1000;
    K80 = [K80 rxPowerW/erinterference80(i,1)];
        end
results{2} = K80';


[m,n] = size(erinterference90);
K90=[];
for i=1:1:m
    rxPowerDbm =CalcRxPower(CalcDistance(sender90(i,1), receiver90(i,1)), PATH_LOSS_EXPONENT); 
    rxPowerW = realpow(10, rxPowerDbm/10)/1000;
    K90 = [K90 rxPowerW/erinterference90(i,1)];
        end
results{3} = K90';

[m,n] = size(erinterference95);
K95=[];
for i=1:1:m
    rxPowerDbm =CalcRxPower(CalcDistance(sender95(i,1), receiver95(i,1)), PATH_LOSS_EXPONENT); 
    rxPowerW = realpow(10, rxPowerDbm/10)/1000;
    K95 = [K95 rxPowerW/erinterference95(i,1)];
        end
results{4} = K95';


group = zeros(0);
dataDisp = zeros(0);
for i = 1 : size(results, 2)
    group = [group ; ones(size(results{i})) + i - 1];
    dataDisp = [dataDisp ; results{i}];
end
figure3 = figure;
boxplot(dataDisp, group, 'notch', 'on');hold on
set(gca,'xtick',[1 2 3 4],'xticklabel',{'70%','80%','90%','95%'},'fontsize',40);
xlabel('Mixed PDR requirement', 'FontSize', 40);
ylabel('PRK parameter', 'FontSize', 40);
grid on;
set(gca,'fontsize',40,'YScale','log');
maximize ();

%==================================================================================
%%%%        Draw the PRK model parameter K in dB
results = {0};
results{1} = 10*log10(K70');
results{2} = 10*log10(K80');
results{3} = 10*log10(K90');
results{4} = 10*log10(K95');

group = zeros(0);
dataDisp = zeros(0);
for i = 1 : size(results, 2)
    group = [group ; ones(size(results{i})) + i - 1];
    dataDisp = [dataDisp ; results{i}];
end
figure4 = figure;
boxplot(dataDisp, group, 'notch', 'on');hold on
set(gca,'xtick',[1 2 3 4],'xticklabel',{'70%','80%','90%','95%'},'fontsize',40);
xlabel('Mixed PDR requirement', 'FontSize', 40);
ylabel('PRK parameter (dB)', 'FontSize', 40);
grid on;
set(gca,'fontsize',40,'YScale','log');
maximize ();
